package demo;


import java.util.Scanner;

public class Main {
    public static int dw = 100;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] s = in.nextLine().split(" ");
        int N = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        good[] goods = new good[m + 1];
        boolean flag = true;
        for (int i = 1; i <= m; i++) {
            String[] s1 = in.nextLine().split(" ");
            int v = Integer.parseInt(s1[0]);
            int p = Integer.parseInt(s1[1]);
            int q = Integer.parseInt(s1[2]);
            if (flag) {
                if (v % dw != 0) {
                    flag = false;
                    dw = 10;
                    for (int j = 1; j < i; j++) {
                        goods[j].setV(goods[j].v * 10);
                    }
                }
            }
            v = v / dw;
            goods[i] = new good(v, p, q);
            if (q > 0) {
                if (goods[q].a1 == 0) {
                    goods[q].setA1(i);
                } else {
                    goods[q].setA2(i);
                }
            }
        }
        N = N /dw;
        dp(N, goods);
    }

    private static void dp(int n, good[] goods) {
        int[][] dp = new int[n + 1][goods.length];
        
    }
}

class good {
    int v;
    int p;
    int q;
    int a1 = 0;
    int a2 = 0;

    public good(int v, int p, int q) {
        this.v = v;
        this.p = p;
        this.q = q;
    }

    public void setV(int v) {
        this.v = v;
    }

    public void setA1(int a1) {
        this.a1 = a1;
    }

    public void setA2(int a2) {
        this.a2 = a2;
    }
}